/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sahd.broker;

import sahd.broker.client.ClientMachine;
import java.util.ArrayList;

/**
 *
 * @author Romms
 */
public class RegisteredClientsRepository {
    ArrayList <ClientMachine> registeredClients;

    public RegisteredClientsRepository(){
        this.registeredClients=new ArrayList <ClientMachine>();
    }

    public boolean clientMachineExists(String clientIpAddress){
        for(int i=0;i<this.registeredClients.size();i++){
            if(this.registeredClients.get(i).getClientMchingeIpAddres().compareTo(clientIpAddress) ==0)
                return true;
        }
        return false;
    }

    public boolean clientMachineExists(ClientMachine clientMachine){
        for(int i=0;i<this.registeredClients.size();i++){
            if(this.registeredClients.get(i).getClientMchingeIpAddres().compareTo(clientMachine.getClientMchingeIpAddres()) ==0)
                return true;
        }
        return false;
    }

    private ClientMachine findClientMachine(ClientMachine clientMachine){
        for(int i=0;i<this.registeredClients.size();i++){
            if(this.registeredClients.get(i).getClientMchingeIpAddres().compareTo(clientMachine.getClientMchingeIpAddres()) ==0)
                return this.registeredClients.get(i);
        }
        return null;
    }

    public boolean registerClientMachine(ClientMachine clientMachine){
        if(clientMachineExists(clientMachine)){
            return false;
        }
        this.registeredClients.add(clientMachine);
        return true;
    }

    public boolean unregisterClientMachine(ClientMachine clientMachine){
        if(clientMachineExists(clientMachine)){
            this.registeredClients.remove(findClientMachine(clientMachine));
            return true;
        }
        return false;
    }


}

